from browser import document
from browser import ajax
from browser import alert
from browser import window


inputHostAddr = document["inputHostAddr"]
inputHostUsername = document["inputHostUsername"]
inputHostPasswd = document["inputHostPasswd"]
inputHostIdentity = document["inputHostIdentity"]
inputHostRemask = document["inputHostRemask"]
saveHostsInfo = document["saveHostsInfo"]

def addHostDatas(req):
    alert(req.text)
    window.location.href = "./index.html?page=hosts"

def saveHosts(ev):
    if ":" not in inputHostAddr.value:
        inputHostAddr.value = inputHostAddr.value + ":22"

    if "" == inputHostUsername.value:
        inputHostUsername.value = "root"

    reqData = {
        "addr": inputHostAddr.value,
        "hostname": inputHostUsername.value,
        "password": inputHostPasswd.value,
        "identity": inputHostIdentity.value,
        "remask": inputHostRemask.value
    }

    request = ajax.Ajax()
    request.bind("complete", addHostDatas)
    request.open("POST", "/manageHosts", False)
    request.send(reqData)

saveHostsInfo.bind("click",saveHosts)